<template>
  <el-dialog :title="'批量修改'" :close-on-click-modal="false" :visible.sync="visible" center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="最大折扣率(0-1)" prop="maxRate">
        <el-input v-model="dataForm.maxRate" placeholder="最大折扣率(0-1)"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { batchEdit } from '@/api/eims/maxdiscount'

  export default {
    data() {
      var exchangeRateCheck = (rule, value, callback) => {
        let numPattern = /^-?\d*\.?\d+$/
        if (!numPattern.test(value)) {
          callback(new Error("请输入阿拉伯数字,含小数，范围(0-1)"));
        }
        if (value > 1 || value < 0) {
          callback(new Error("请输入0-1之间数字"));
        } else {
          callback()
        }
      };
      return {
        ischeck: false,
        visible: false,
        dataForm: {
          ids: [],
          maxRate: '',
        },
        dataRule: {
          maxRate: [{
              required: true,
              message: '最大折扣率不能为空',
              trigger: 'blur'
            },
            {
              validator: exchangeRateCheck
            }
          ]
        }
      }
    },
    methods: {
      init(ids) {
        this.dataForm.ids = ids
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            batchEdit(this.dataForm).then(response => {
              if(response.data.code == '0'){
                this.$message.success('修改成功')
                this.visible = false
                this.$emit('refreshDataList')
              }else{
                this.$message.error(response.data.msg)
              }
              this.ischeck = false
            }).catch(() => {
              this.ischeck = false
            })
          }else{
            this.ischeck = false
          }
        })
      }
    }
  }
</script>
